package com.ruoyi.project.wx.service.impl;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.bootdo.PageHelperUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.project.wx.dao.GoodsClassDao;
import com.ruoyi.project.wx.domain.GoodsClassDO;
import com.ruoyi.project.wx.service.GoodsClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class GoodsClassServiceImpl implements GoodsClassService {
	@Autowired
	private GoodsClassDao goodsClassDao;
	
	@Override
	public GoodsClassDO get(Integer id){
		return goodsClassDao.get(id);
	}
	
	@Override
	public List<GoodsClassDO> list(Map<String, Object> map){
		return goodsClassDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return goodsClassDao.count(map);
	}
	
	@Override
	public int save(GoodsClassDO goodsClass){
		return goodsClassDao.save(goodsClass);
	}
	
	@Override
	public int update(GoodsClassDO goodsClass){
		return goodsClassDao.update(goodsClass);
	}
	
	@Override
	public int remove(String id){
		return goodsClassDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return goodsClassDao.batchRemove(ids);
	}
	/**
	 * 获取一级分类
	 */
	@Override
	public R classList(Map<String, Object> map) {
		List<GoodsClassDO> goodsClass = goodsClassDao.classList(map);
		return R.ok().put("data", goodsClass);
	}

	@Override
	public R queryByPrentId(Map<String, Object> params) {
		PageHelperUtils.page(params);
		List<Map<String,Object>> goodsClassList = goodsClassDao.queryByPrentId(params);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(goodsClassList);
		return R.ok().put("data", pageInfo);
	}

	@Override
	public List<GoodsClassDO> queryAll() {
		return goodsClassDao.queryAll();
	}

//	@Override
//	public Tree<GoodsClassDO> getTree() {
//		List<Tree<GoodsClassDO>> trees = new ArrayList<Tree<GoodsClassDO>>();
//		List<GoodsClassDO> classDOs = goodsClassDao.list(new HashMap<>(16));
//		for (GoodsClassDO goodsClassDO : classDOs) {
//			Tree<GoodsClassDO> tree = new Tree<GoodsClassDO>();
//			tree.setId(goodsClassDO.getId());
//			tree.setParentId(goodsClassDO.getParentId());
//			tree.setText(goodsClassDO.getName());
//			trees.add(tree);
//		}
//		// 默认顶级大类为０，根据数据库实际情况调整
//		Tree<GoodsClassDO> t = BuildTree.build(trees);
//		return t;
//	}
//
    @Override
    public R getClassList(Map<String, Object> map) {
        PageHelperUtils.page(map);
		List<Map<String,Object>> list = goodsClassDao.getBigList(map);
		if(list.size()>0){
			for (Map<String,Object> map1 : list) {
				map.put("parentId",map1.get("id").toString());
				List<Map<String,Object>> goodsClassList = goodsClassDao.queryByPrentId(map);
				map1.put("smallList",goodsClassList);
			}
		}
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		return R.ok().put("data",pageInfo);

	}

}
